"""
@name : comments
@author : FanMY_71
@projectname: Selling-Idea
@Time: 2022/9/5-20:44
"""
from flask import Blueprint, request
from flask_restful import Api, Resource

from libs.handler import default_error_handler
from libs.response import generate_response
from model import db
from model.tb_com import tb_com

comment_bp = Blueprint("comment", __name__, url_prefix="/v1")
api = Api(comment_bp)

class CommentsView(Resource):
    def post(self):
        """
        新增评论
        输入：用户id、父评论id、文章id、评论内容
        :return:message=评论成功，data=文章id
        """
        com = tb_com()
        com.user_id = request.json.get("user_id")
        com.article_id = request.json.get("article_id")
        com.comment = request.json.get("comment")
        father_id = request.json.get("father_id")
        if father_id:
            com.father_id = father_id
        db.session.add(com)
        db.session.commit()
        return generate_response(message="create comment success", data={"article_id":com.article_id})


api.add_resource(CommentsView, "/comments")
# api.handle_error = default_error_handler